home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / x2b11.zip / X2B11.ASM < prev   
Assembly Source File  |  1989-10-14  |  18KB  |  568 lines

  1. Comment    ~
  2.     X2B.ASM
  3.     A replacement for the Exe2Bin program, which is NOT included with
  4.     PC-DOS 3.3.
  5.     written by Henry T. Nettles, Dec. 21, 1987 -- Feb. 20, 1988
  6.     compiled with MicroSoft Macro Assembler, version 5.0
  7.     The inspiration (and model) for this program was EXE2COM.C, written
  8.     by Chris Dunford.
  9.     The include module, DOS.INC, comes with MASM 5.0
  10.     The subroutine BinToStr is borrowed from the SHOW.ASM program which
  11.     also comes with MASM 5.0
  12.     Some of this code (the command line parser) is borrowed from a program
  13.     by Vernon Buerg (TABS.ASM)
  14.     This program is hereby released to the public domain.
  15.  
  16.     Usage:  >X2B  [d:][\path\]PROG[.EXE]  [d:][\path\][PROG][.COM]
  17.  
  18.     The only necessary parameter is the file name of the input file.  A
  19.     drive and/or path may be given if needed.
  20.     If no extension is given, .EXE is assumed.  If no ouput file is
  21.     given, then the same file name with an extension of .COM is used.
  22.     WARNING: If a drive and/or path is given for the input file, and
  23.     no ouput file is given, then the input drive and/or path will be
  24.     used for the ouput (not the default directory).
  25.  
  26.     This program works on my computer, on the files I have tried it on.
  27.     However, extensive testing has not been performed, and I assume no
  28.     responsibility whatever for the program.  I would be interested in
  29.     bug reports, and if anyone improves the code it would be nice to
  30.     receive a copy.  I can be reached at:
  31.  
  32.             22547 Braken Carter
  33.             Katy, Texas 77449-3619
  34.  
  35. Toad Hall Tweak, 15 Oct 89
  36. - Converted to .COM format
  37. - General tightening
  38. - Moved EXE header buffer, filename buffers to dynamic space at code end
  39.   (to reduce program size)
  40. - Changed BinToStr procedure so it carries parms directly in AX/DI
  41.   instead of the slow pushes.
  42. - Removed some unnecessary local variables
  43.  
  44. Comment    ends    ~
  45.  
  46. RECSIZE        equ    512
  47. CMDTAIL        equ    80h
  48. CR        equ    0dh
  49. LF        equ    0ah
  50. EOM        equ    '$'
  51. SIZE_OF_HEADER    equ    28
  52.  
  53. exe_header    STRUC        ; what an exe header looks like
  54. exe_sig1    db    0    ; EXE file signature: "MZ"
  55. exe_sig2    db    0
  56. excess        dw    0    ; image size mod 512 (valid bytes in last page)
  57. pages        dw    0    ; # of 512 byte pages in image
  58. relo_ct        dw    0    ; count of relocation table entries
  59. hdr_size    dw    0    ; size of header in paragraphs
  60. min_mem        dw    0    ; min required memory
  61. max_mem        dw    0    ; max required memory
  62. xss        dw    0    ; stack seg offset in load module
  63. xsp        dw    0    ; initial value of sp
  64. cksum        dw    0    ; file checksum
  65. xip        dw    0    ; initial value of IP
  66. xcs        dw    0    ; cs offset in load module
  67. relo_start    dw    0    ; offset of first relocatable item
  68. ovl_num        dw    0    ; overlay number
  69. exe_header ENDS            ; end of structure
  70.  
  71. CSEG    SEGMENT PARA PUBLIC 'CODE'
  72.     ASSUME    CS:CSEG, DS:CSEG, ES:CSEG
  73.  
  74.     org    100H
  75.  
  76. X2B    proc    near
  77.     jmp    Start        ;skip over runtime data            v1.1
  78.  
  79. errflag        db    0    ;error flag                v1.1
  80. save_ip        dw    0
  81. handle1        dw    0
  82. handle2        dw    0
  83.  
  84. msg1    db    'Input File ==>',eom
  85. msg2    db    '  Output File ==>',eom
  86. msg3    db    CR,LF,EOM
  87.  
  88. author        db    'X2B 1.1  Public Domain Software by Henry T. Nettles'
  89.         db    CR,LF,EOM
  90. code_size_msg    db    'Code Size = '
  91. code_size_str    db    '      ',eom
  92. code_start_msg    db    '  Code Start = '
  93. code_start_str    db    '      ',eom
  94. ip_msg        db    '  Initial IP = '
  95. ip_str        db    '      ',CR,LF,EOM
  96. actual_code_size_msg db    'Size of .Com file = '
  97. actual_code_size_str db    '      ',CR,LF,EOM
  98.  
  99. code_size dw    0        ; amount of code to move to .com file
  100. code_start dw    0
  101.  
  102. no_file_msg1 db    'USAGE:  >X2B  [d:][\path\]file[.exe] [d:][\path\][file][.com]'
  103.          db    CR,LF,EOM
  104. open_fail_msg  db 'ERROR: Open of input file failed!',CR,LF,EOM
  105. create_fail_msg    db 'ERROR: Create of output file failed!',CR,LF,EOM
  106. disk_full_msg  db 'ERROR: The disk is full!',CR,LF,EOM
  107. Bad_Read_msg   db 'I/O Error on Read!',CR,LF,EOM
  108. bad_write_msg  db 'I/O Error on Write!',CR,LF,EOM
  109. seek_error_msg db 'I/O Error on Seek!',CR,LF,EOM
  110.  
  111. badsig_msg     db 'Invalid EXE File Signature',CR,LF,EOM
  112. hasrelo_msg    db 'EXE has relocatable items',CR,LF,EOM
  113. has_ss_msg     db 'EXE has stack segment',CR,LF,EOM
  114. bad_ip_msg     db 'IP not 0 or 100h',CR,LF,EOM
  115. too_big_msg    db 'Exe file is too big to convert to COM file',CR,LF,EOM
  116. mul_err_msg    db 'Overflow on multiply',CR,LF,EOM
  117.  
  118.  
  119. Start:                    ;v1.1
  120.  
  121. ;****************************************************************************
  122. ;*    Get two file names from command line
  123. ;*    This portion of code was borrowed from TABS.ASM by Vernon Buerg
  124. ;****************************************************************************
  125.  
  126.     xor    al,al            ;handy 0            v1.1
  127.     mov    FNAME1,al        ;insure both dynamic filename    v1.1
  128.     mov    FNAME2,al        ; buffers are 0            v1.1
  129.  
  130.     mov    si,CMDTAIL        ; DS:SI points to command line
  131.     sub    bp,bp            ;Indicates first or second name
  132.     sub    ch,ch            ;The PSP may contain one or two
  133.     or    cl,[si]            ; filenames separated by blanks v1.1
  134.     jz    GetF5            ; First byte of cmdline should not be 0
  135.  
  136.     mov    di,offset FNAME1    ;ES:DI points to fname1        v1.1
  137.     Inc    si            ; point to next char from command line
  138.                     ; (assume that 1st char is a blank)
  139.  
  140. GetF1:    Lodsb                ;Copy command line to file names
  141.                     ; AL will contain character pointed
  142.                     ; to by DS:SI
  143.     cmp    AL,' '            ; skip leading blanks
  144.     jne    GetF2            ; not a blank -- jump
  145.  
  146.     or    bp,bp            ; or until the length is zero
  147.     jz    GetF4            ;If a second blank is found,
  148.      mov    ax,2400h        ; append zero and dollar sign
  149.      stosw                ; mov AX to ES:DI
  150.      mov    di,offset FNAME2    ;ES:DI now points to 2d filename v1.1
  151.      jmp    short GetF4
  152.  
  153. GetF2:    cmp    AL,Cr            ;Is it CR, end of line?
  154.     je    GetF5            ; yes, end of command
  155.  
  156.     stosb                ; no, save in name
  157.     mov    bp,di            ; and indicate data copied
  158. GetF4:    loop    GetF1
  159.  
  160. GetF5:    mov    ax,2400h        ;Append zero and dollar    sign
  161.     stosw
  162.  
  163.     mov    dx,offset author    ;tell them who we are        v1.1
  164.     mov    ah,9            ;display string
  165.     int    21H
  166.  
  167. ;***************************************************************************
  168. ;*    DISPLAY THE TWO FILE NAMES FROM COMMAND LINE
  169. ;***************************************************************************
  170.     mov    si,offset FNAME1    ;point to fname1         v1.1
  171.     xor    al,al            ;handy 0            v1.1
  172.     cmp    al,[si]            ;did user enter at least 1 file name? v1.1
  173.     jnz    L1            ; is ok, we have a file name
  174.      jmp    No_File1
  175.  
  176. L1:
  177.     mov    di,si    ;offset fname1    ; check fname1 for extension    v1.1
  178.     mov    cx,-1
  179.     cld                ; direction of search = forward
  180.     repnz    scasb            ; search through file name for '\0'
  181.     not    cx            ; CX = length including '\0'
  182.     mov    dx,cx            ;save length in DX a sec    v1.1
  183.     mov    di,si    ;offset fname1    ;point back to fname1 start    v1.1
  184.     mov    al,'.'
  185.     repnz    scasb            ; search for period in file name
  186.     jz    L1a            ; found the period, must have ext
  187.  
  188.     mov    ax,si    ;offset fname1    ; no period, must add .EXE for user v1.1
  189.     add    ax,dx    ;fname1_len    ;                v1.1
  190.     dec    ax            ;
  191.     mov    di,ax            ; di points to '\0' at end of file name
  192.     mov    ax,'E.'    ;452eh        ; ".E" backwards        v1.1
  193.     stosw
  194.     mov    ax,'EX'    ;4558h        ; "XE" backwards        v1.1
  195.     stosw
  196.     mov    ax,2400h        ; append zero and dollar sign
  197.     stosw
  198.  
  199. L1a:
  200.     mov    cx,-1            ;common code            v1.1
  201.     cmp    byte ptr FNAME2,0    ;did user enter 2d file name?    v1.1
  202.                     ; (should not be 0)        v1.1
  203.     jnz    L2            ; fname2 is non-blank, go check    for ext
  204.  
  205.     mov    si,offset FNAME1    ;source is FNAME1        v1.1
  206.     mov    di,si    ;offset FNAME1    ;into DI for a scasb        v1.1
  207.     mov    al,'.'
  208.     cld
  209.     repnz    scasb            ; search fname1 for '.'
  210.     not    cx            ; cx has length of fname1 including '.'
  211.     dec    cx            ; don't copy the period
  212.     mov    di,offset FNAME2    ; let Dest.  Index point to fname2 v1.1
  213.                     ;SI (source) is already FNAME1    v1.1
  214.     cld                ; direction of movement = forward
  215.     rep    movsb            ; move chars from [SI] to [DI]
  216.                     ;   CX has count (# chars to move)
  217.     jmp    short L2a        ; go add the ".COM"
  218. ;
  219. L2:    mov    di,offset fname2    ; check fname2 for extension
  220.     xor    al,al            ; zero al
  221.     cld                ; direction of search = forward
  222.     repnz    scasb            ; search through file name for '\0'
  223.     not    cx            ; CX = length including '\0'
  224.     mov    dx,cx            ; save the length in DX a sec    v1.1
  225.     mov    di,offset FNAME2        ;v1.1
  226.     mov    al,'.'
  227.     repnz    scasb            ; search for period in file name
  228.     jz    L2b            ; found the period, must have ext
  229.  
  230.     mov    ax,offset FNAME2        ;v1.1
  231.     add    ax,dx    ;fname2_len    ;add in FNAME2's length        v1.1
  232.     dec    ax            ;adjust
  233.     mov    di,ax            ; di points to '\0' at end of file name
  234. L2a:    mov    ax,'C.'    ;432eh        ; ".C" backwards        v1.1